/*
 * designassignment3.c
 *
 * Created: 3/14/2014 8:33:05 AM
 *  Author: Yosemit Xolalpa Rosales
 */ 
#include <avr/io.h>
#include <avr/interrupt.h>

int volatile ctrInt = 0;

int main(void)
{

	DDRD = 0b11000000;		//SET PD6 and PD7 as output
	DDRB = 0xFF;			//SET ENTIRE Port B as output for counter

	TCCR1B = 0b00001011;	//CTC mode prescaler 64
	TIMSK1 = 0b00000010;	//Timer1 interrupt mask register
	OCR1A = 7812;

	TCCR0A = 0b00000000;
	TCCR0B = 0b00000111;	//Set external clock source for Timer0
	EIMSK = 0x01;			//Enable INT0 as interrupt
	EICRA = 0x02;			//Trigger on a falling edge
	sei();					//ENABLE INTERRUPTS!
	
	while(1)
	{
							//wait for interrupt
		PORTB = TCNT0;		//output the count to Port B
	}
}

ISR (INT0_vect)
{
							//Called every falling edge of INT0
	ctrInt++;				//inc counter
	if (ctrInt == 16)		//are we at 16 "falling edges" yet?
	{
		ctrInt = 0;			//reset counter
		PORTD ^= (1<<PD7);  //output to 9th bit LED
	}
}


ISR (TIMER1_COMPA_vect)
{
	TCNT1 = 0;
	PORTD ^= (1<<PD6);		//Toggle PD6, connect to PD4 to drive...
							//...Timer0's clock input (since we've...
							//... asked for an external clock).
}
